using UnityEngine;
using QFramework;
using Cinemachine;
using System.Collections.Generic;

public class PropSetSystem : MonoBehaviour,ISystem
{
    public bool Initialized { get; set ; }
    public CinemachineVirtualCamera VirtualCamera;
    public CinemachineConfiner2D confiner2D;
    public float speed = 0.1f;
    public float scrollSpeed = 0.1f;
    public GameObject Wall;
    private PropData propData;
    private  GameObject CurrentProp;
    List<GameObject> StageProp = new List<GameObject>();
    
    public bool GameStartFlag;
    public void MouseGetProp(PropData propData)
    {
        this.propData = propData;
        var propPrefab = propData.prefab;
        CurrentProp = GameObject.Instantiate(propPrefab);
    }

    private void Update() 
    {
        if(GameStartFlag == false)
        {
            var mousePosition = Input.mousePosition;
            if(CurrentProp != null)
            {   
                var mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
                CurrentProp.transform.position = new Vector3(mouseWorldPosition.x,mouseWorldPosition.y,transform.position.z);
                if(Input.GetMouseButton(0))
                {
                    StageProp.Add(CurrentProp);
                    CurrentProp = null;
                    this.SendEvent<PropSet>(new PropSet()
                    {
                        propData = this.propData
                    });
                }
                if(Input.GetMouseButton(1))
                {
                    GameObject.DestroyImmediate(CurrentProp);
                    CurrentProp = null;
                }
            }
            Vector3 deltaVec = Vector3.zero;

            if(Input.GetKey(KeyCode.A))
            {
                deltaVec = Vector3.left;
            }
            else if(Input.GetKey(KeyCode.D))
            {
                deltaVec = Vector3.right;
            }
            else if(Input.GetKey(KeyCode.S))
            {
                deltaVec = Vector3.down;
            }
            else if(Input.GetKey(KeyCode.W))
            {
                deltaVec = Vector3.up;
            }
            else
            {
                deltaVec = Vector3.zero;
            }
            float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
            if(mouseScrollWheel > 0 && VirtualCamera.m_Lens.OrthographicSize > 5)
            {
                VirtualCamera.m_Lens.OrthographicSize -= scrollSpeed;
            }
            else if (mouseScrollWheel < 0 && VirtualCamera.m_Lens.OrthographicSize < 10)
            {
                VirtualCamera.m_Lens.OrthographicSize += scrollSpeed;
            }
            VirtualCamera.transform.position = VirtualCamera.transform.position + deltaVec * speed;
        }
        if(Input.GetKey(KeyCode.R))
        {
            if(CurrentProp != null)
            {
                GameObject.DestroyImmediate(CurrentProp);
                CurrentProp = null;
            }
            while(StageProp.Count > 0)
            {
                GameObject.DestroyImmediate(StageProp[0]);
                StageProp.RemoveAt(0);
            }
            this.SendEvent<PropReset>();
        }
    }
    private void OnGameStart(GameStart _)
    {
        VirtualCamera.enabled = false;
    }

    public void SetArchitecture(IArchitecture architecture) {}

    public IArchitecture GetArchitecture() => GameArchitecture.Interface;

    public void Init()
    {
        GameStartFlag = false;
        GetArchitecture().GetSystem<Stage>().Confiner2D = confiner2D;
        GetArchitecture().GetSystem<Stage>().RegisterEvent<GameStart>(OnGameStart);
    }

    public void Deinit()
    {
        
    }
}